<?php

namespace api\modules\goods\controllers;

use api\extensions\ApiBaseController;
use api\services\goods\GoodsService;
use api\services\goods\GoodsCollectService;

class GoodsController extends ApiBaseController
{

    /**
     * 商品列表
     * * */
    public function actionList() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['brand_id', 'cate_id', 'keyword'], 'default', 'value' => '', 'message' => '搜索参数'],
            [['page', 'first', 'price', 'sales'],  'default', 'value' => '', 'message' => '搜索参数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }

        return GoodsService::getList($this->params);
    }

    /**
     * 商品详情
     * * */
    public function actionDetail() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }

        return GoodsService::getDetail($this->params['id']);
    }

    /**
     * 商品图文详情
     * * */
    public function actionDesc() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }

        return GoodsService::getDesc($this->params['id']);
    }

    /**
     * 商品收藏
     * * */
    public function actionCollect() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return GoodsCollectService::collect($this->params);
    }

    /**
     * 商品收藏列表
     * * */
    public function actionCollectList() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['page'], 'default', 'value' => 1, 'message' => '页数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return GoodsCollectService::getList($this->params);
    }

    /**
     * 商品浏览列表
     * * */
    public function actionHistory() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['page'], 'default', 'value' => 1, 'message' => '页数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return GoodsService::history($this->params);
    }
}
